package sdk.webview.fmc.com.fmcsdk.base;

import android.content.Context;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import sdk.webview.fmc.com.fmcsdk.R;

/**
 * Created by ydm on 2018/6/6.
 */

public class UploadLogAdapter extends CommonAdapter  {
    List<Map<String, Object>> filesList;
    private int thePosition;
    private CheckBoxCallBack checkBoxCallBack;

    private Map<Integer,Boolean> map=new HashMap<>();// 存放已被选中的CheckBox


    public interface CheckBoxCallBack {
        void   selectCheckBox(boolean isChecked, int position);
    }
    public UploadLogAdapter(Context context, List mDatas, int itemLayoutId, CheckBoxCallBack checkBoxCallBack) {
        super(context, mDatas, itemLayoutId);
        this.checkBoxCallBack = checkBoxCallBack;
        filesList = mDatas;
    }

    @Override
    public void convert(ViewHolder helper, Object item, final int position) {
        thePosition = position;
        CheckBox checkBox = helper.getView(R.id.cb);
        TextView tv_name = helper.getView(R.id.tv_name);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked==true){
                    map.put(position,true);
                }else {
                    map.remove(position);
                }
                if (checkBoxCallBack!=null){
                    checkBoxCallBack.selectCheckBox(isChecked,position);
                }
            }
        });
        if(map!=null&&map.containsKey(position)){
            checkBox.setChecked(true);
        }else {
            checkBox.setChecked(false);
        }
        tv_name.setText(filesList.get(position).get("filename").toString());

    }


}
